<template>
  <NuxtLayout name="admin" menuActive="/admin/certificate">
    <div class="card">
      <el-page-header @back="useRouter().go(-1)"></el-page-header>
      <div style="height: 18px;"></div>
      <el-table :data="tableData" border show-overflow-tooltip>
        <el-table-column prop="institutionFullName" label="机构全称" />
        <el-table-column prop="institutionShortName" label="机构简称" />
        <el-table-column prop="classesName" label="班级名称" />
        <el-table-column prop="idName" label="学生名称" />
        <el-table-column prop="certTime" label="发证时间" />
        <el-table-column label="操作" fixed="right" width="140">
          <template #default="scope">
            <a class="el-button el-button--primary el-button--small" :href="scope.row.certUrl" target="_blank">查看</a>
            <el-button type="danger" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="card_pagin">
        <el-pagination background layout="prev, pager, next, total" :total="total" :page-size="listForm.pageSize" @change="pageChangeFn" />
      </div>
    </div>
  </NuxtLayout>
</template>

<script setup>
import { userCertificate, deleteCertificate } from '~/server/api'
// 验证路由
definePageMeta({
  validate: async (route) => {
    return /^1\d{5,}$/.test(route.params.userId);
  }
})
// TDK
const prevHost = useRequestHeaders()["x-forwarded-proto"];
const host = useRequestHeaders()["host"];
const fullPath = useRoute()["fullPath"];
useSeoMeta({
  title: "技考邦_证书管理",
  keywords: "技考邦,证书管理",
  description: "技考邦，证书管理。",
  ogTitle: "技考邦_证书管理",
  ogDescription: "技考邦，证书管理。",
  ogType: "article",
  ogUrl: `${prevHost}://${host}${fullPath}`,
});
// 列表
const listForm = ref({
  pageNum: 1,
  pageSize: 20,
  userId: useRoute().params.userId
})
const tableData = ref([])
const total = ref(0)
const getList = async () => {
  let { data: listRes } = await useAsyncData(() => useServerRequest(userCertificate, {
    method: 'post',
    body: JSON.stringify(listForm.value)
  }))
  if (listRes.value && listRes.value.errno === 0) {
    tableData.value = listRes.value.result.list;
    total.value = listRes.value.result.total;
  }
}
getList()
// 分页
const pageChangeFn = (val) => {
  listForm.value.pageNum = val;
  getList()
}
// 删除
const delFn = (val) => {
  ElMessageBox.confirm('是否删除当前数据', {
    showClose: false,
    closeOnClickModal: false,
    closeOnPressEscape: false
  }).then(async () => {
    let delRes = await useServerRequest(deleteCertificate, {
      method: 'post',
      body: JSON.stringify({
        id: val.id
      })
    })
    if (delRes.errno === 0) {
      ElMessage({
        message: '删除成功',
        type: 'success'
      })
      getList()
      return false;
    }
  }).catch(() => {})
}
</script>

<style lang="less" scoped>
.card {
  background: #FFFFFF;
  border-radius: 10px;
  border: 1px solid #EEEEEE;
  padding: 35px 25px;

  &_pagin {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>